package com.dms.web.controller.system;

import java.util.*;

import com.dms.common.core.domain.entity.SysUser;
import com.dms.system.domain.SysPlant;
import com.dms.system.domain.SysTodayMission;
import com.dms.system.domain.dto.SysPlantRecordDTO;
import com.dms.system.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.dms.common.annotation.Log;
import com.dms.common.enums.BusinessType;
import com.dms.system.domain.SysRecord;
import com.dms.common.core.controller.BaseController;
import com.dms.common.core.domain.AjaxResult;
import com.dms.common.utils.poi.ExcelUtil;
import com.dms.common.core.page.TableDataInfo;

/**
 * 巡检记录，巡检人员上传现场数据Controller
 * 
 * @author ruoyi
 * @date 2021-03-27
 */
@Controller
@RequestMapping("/system/record")
public class SysRecordController extends BaseController
{
    private String prefix = "system/record";

    @Autowired
    private ISysRecordService sysRecordService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysPlantService sysPlantService;

    @Autowired
    private ISysMissionService sysMissionService;

    @Autowired
    private ISysTodayMissionService sysTodayMissionService;

    @GetMapping()
    public String record(@RequestParam(defaultValue = "200") Long deptId, Model m)
    {
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(deptId);
        m.addAttribute("userList",sysUserService.selectUserList(sysUser));
        m.addAttribute("plantList",sysPlantService.selectSysPlantList(null));
        return prefix + "/record";
    }

    /**
     * 查询巡检记录，巡检人员上传现场数据列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysRecord sysRecord)
    {
        startPage();
        List<SysRecord> list = sysRecordService.selectSysRecordList(sysRecord);
        return getDataTable(list);
    }

    /**
     * 导出巡检记录，巡检人员上传现场数据列表
     */
    @Log(title = "巡检记录，巡检人员上传现场数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysRecord sysRecord)
    {
        List<SysRecord> list = sysRecordService.selectSysRecordList(sysRecord);
        ExcelUtil<SysRecord> util = new ExcelUtil<SysRecord>(SysRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 新增巡检记录，巡检人员上传现场数据
     */
    @GetMapping("/add")
    public String add(Model m)
    {
        m.addAttribute("plantList",sysPlantService.selectSysPlantList(null));
        return prefix + "/add";
    }

    @GetMapping("/pressure")
    public String pressure(Model m,SysRecord sysRecord){
        sysRecord.setRecordTime(new Date());
        // 只查询状态为正常的设备的温度
        SysPlant sysPlant = new SysPlant();
        sysPlant.setPlantStatus("1");
        List<SysPlant> plantList = sysPlantService.selectSysPlantList(sysPlant);
        m.addAttribute("plantList",plantList);
        m.addAttribute("recordDTOList",sysRecordService.selectSysPlantRecordDTOList(sysRecord,true));
        return prefix + "/pressure";
    }

    @GetMapping("/temperature")
    public String temperature(Model m,SysRecord sysRecord){
        sysRecord.setRecordTime(new Date());
        // 只查询状态为正常的设备的温度
        SysPlant sysPlant = new SysPlant();
        sysPlant.setPlantStatus("1");
        List<SysPlant> plantList = sysPlantService.selectSysPlantList(sysPlant);
        m.addAttribute("plantList",plantList);
        m.addAttribute("recordDTOList",sysRecordService.selectSysPlantRecordDTOList(sysRecord,true));
        return prefix + "/temperature";
    }

    /** 获取每分钟数据 */
    @ResponseBody
    @GetMapping("/getData")
    public List<SysPlantRecordDTO> getPlantRecordList(SysRecord sysRecord){
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        sysRecord.setRecordTime(calendar.getTime());
        return sysRecordService.selectSysPlantRecordDTOList(sysRecord,false);
    }

    /** 获取历史数据 */
    @ResponseBody
    @GetMapping("getHistory")
    public List<SysPlantRecordDTO> getHistory(SysRecord sysRecord){
        return sysRecordService.selectSysPlantRecordDTOList(sysRecord,true);
    }

    /**
     * 新增保存巡检记录，巡检人员上传现场数据
     * 同时修改设备状态
     */
    @Log(title = "巡检记录，巡检人员上传现场数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysRecord sysRecord)
    {
        sysRecord.setRecordTime(new Date());
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysRecord.setUserId(sysUser.getUserId());
        SysPlant sysPlant = new SysPlant();
        sysPlant.setId(sysRecord.getPlantId());
        sysPlant.setPlantStatus(sysRecord.getStatus());
        // 修改设备状态，并查润巡检记录
        return toAjax(sysPlantService.updateSysPlant(sysPlant)*sysRecordService.insertSysRecord(sysRecord));
    }

    /**
     * 修改巡检记录，巡检人员上传现场数据
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysRecord sysRecord = sysRecordService.selectSysRecordById(id);
        mmap.put("sysRecord", sysRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存巡检记录，巡检人员上传现场数据
     */
    @Log(title = "巡检记录，巡检人员上传现场数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysRecord sysRecord)
    {
        SysPlant sysPlant = new SysPlant();
        sysPlant.setId(sysRecord.getPlantId());
        sysPlant.setPlantStatus(sysRecord.getStatus());
        return toAjax(sysPlantService.updateSysPlant(sysPlant)*sysRecordService.updateSysRecord(sysRecord));
    }

    /**
     * 删除巡检记录，巡检人员上传现场数据
     */
    @Log(title = "巡检记录，巡检人员上传现场数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysRecordService.deleteSysRecordByIds(ids));
    }

    /**
     * 获取员工今天打卡的设备记录
     * 用来判断任务是否完成
     * */
    @ResponseBody
    @GetMapping("/checkedPlants")
    public Set<Long> checkedPlants(SysRecord sysRecord){
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysRecord.setUserId(user.getUserId());
        sysRecord.setRecordTime(new Date());
        List<SysRecord> recordList = sysRecordService.selectSysRecordList(sysRecord);
        HashSet<Long> hashSet = new HashSet<>();
        for (SysRecord record : recordList) {
            hashSet.add(record.getPlantId());
        }
        return hashSet;
    }

    /** 检查任务是否完成 */
    @ResponseBody
    @GetMapping("/checkMission")
    public boolean checkMission(Long missionId,Long todayMissionId){
        // 任务是否完成标志
        // 任务完成：需要检查的设备编号都在有记录的设备编号里
        boolean flag=true;
        // 有记录的设备编号集合
        Set<Long> checkedPlants = checkedPlants(new SysRecord());
        // 需要检查的设备编号集合
        List<Long> missionPlantList = sysMissionService.selectPlantIdsByMissionId(missionId);
        for (Long pid : missionPlantList) {
            if(!checkedPlants.contains(pid)){
                flag=false;
                break;
            }
        }
        if(flag){
            SysTodayMission todayMission = new SysTodayMission();
            todayMission.setTodayMissionId(todayMissionId);
            todayMission.setMissionIscomplete(1);
            sysTodayMissionService.updateSysTodayMission(todayMission);
        }
        return flag;
    }
}
